﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BS_2.VTKClass
{
    class DataFactory
    {
        static int bs = 8;
        ContourZone contourZone;
        float vbb1;
        int indexVal;
        Dictionary<String, List<float>> vars;
        Form1 mianform;
        bool istiwangge;
        object[] data;

        float xlen, ylen, zlen;
        public void setData(object[] data)
        {
            this.data = data;
            contourZone = (ContourZone)data[0];
            vbb1 = (float)data[1];
            indexVal = (int)data[2];
            vars = (Dictionary<String, List<float>>)data[3];
            mianform = (Form1)data[4];
            istiwangge = (bool)data[5];
            xlen = contourZone.getXYZLen()[0] / 2;
            ylen = contourZone.getXYZLen()[1] / 2;
            zlen = contourZone.getXYZLen()[2] / 2;
        }

        public void getPointsAndCell()
        {
            if (istiwangge)
            {
                // borderQJ();
                //   borderNewLimit();
                new SerializeFace().borderNewLimit(data);
            }
            else
            {
                Face();
            }

        }

        public void borderQJ()
        {
            String varName = mianform.varNow;
            float max = mianform.varlimitData[1];
            float min = mianform.varlimitData[0];


            ZoneData zoneData = contourZone.zoneData;
            Zone zone = new Zone();
            Dictionary<String, List<float>> varMap = zone.getVarMap();
            Dictionary<String, List<int>> varCiShuMap = zone.getVarCiShuMap();
            List<P3D> points = zone.getPoints();
            List<Face> faces = zone.getFaces();
            Dictionary<String, List<float>> faceCenterVar = zone.getfaceCenterValueMap();
            contourZone.zones.Add(zone);

            String[] varNames = vars.Keys.ToArray();

            for (int i = 0; i < varNames.Count(); i++)
            {
                //k 叠加的值 v 叠加次数
                List<float> list = new List<float>();
                varMap.Add(varNames[i], list);
                List<int> cishulist = new List<int>();
                varCiShuMap.Add(varNames[i], cishulist);
                List<float> listcenter = new List<float>();
                faceCenterVar.Add(varNames[i], listcenter);
            }

            bool boolean = false;
            for (int i = 0; i < zoneData.centerX_LS.Count(); i++)
            {

                float val = vars[varName][i];//当前单元的中心变量值
                if (val > max || val < min)//获取区间单元
                {
                    continue;
                }

                float x = zoneData.centerX_LS[i];
                float y = zoneData.centerY_LS[i];
                float z = zoneData.centerZ_LS[i];

                float x1 = x + xlen;
                float y1 = y + ylen;
                float z1 = z + zlen;

                float x2 = x - xlen;
                float y2 = y - ylen;
                float z2 = z - zlen;

                P3D[] p3ds = new P3D[bs];
                p3ds[0] = new P3D(x1, y1, z1);
                p3ds[1] = new P3D(x1, y1, z2);
                p3ds[2] = new P3D(x1, y2, z2);
                p3ds[3] = new P3D(x1, y2, z1);

                p3ds[4] = new P3D(x2, y1, z1);
                p3ds[5] = new P3D(x2, y1, z2);
                p3ds[6] = new P3D(x2, y2, z2);
                p3ds[7] = new P3D(x2, y2, z1);
                int[] pointsIndex = new int[bs];


                //异次去重法
                for (int k = 0; k < p3ds.Count(); k++)
                {
                    P3D p3d = p3ds[k];
                    pointsIndex[k] = points.Count();
                    points.Add(p3d);
                    //非重复顶点下 添加当前单元的变量值
                    List<float> floats = varMap[varName];
                    floats.Add(val);
                    List<int> indexList = varCiShuMap[varName];
                    indexList.Add(1);//叠加次数加1 
                }

                //生成单元索引
                //face 7
                Face face7 = new Face(
                        pointsIndex[2],
                        pointsIndex[3],
                        pointsIndex[7],
                        pointsIndex[6]);
                //bool  contains7 = faces.Contains(face7);
                //if (!contains7)
                faces.Add(face7);

                //face 6
                Face face6 = new Face(
                        pointsIndex[1],
                        pointsIndex[2],
                        pointsIndex[6],
                        pointsIndex[5]);
                //bool contains6 = faces.Contains(face6);
                //if (!contains6)
                faces.Add(face6);


                //face 4
                Face face4 = new Face(
                        pointsIndex[0],
                        pointsIndex[4],
                        pointsIndex[5],
                        pointsIndex[1]);
                //bool  contains4 = faces.Contains(face4);
                //if (!contains4)
                faces.Add(face4);
                //face 3
                Face face3 = new Face(
                        pointsIndex[0],
                        pointsIndex[3],
                        pointsIndex[7],
                        pointsIndex[4]);
                //bool  contains3 = faces.Contains(face3);
                //if (!contains3)
                faces.Add(face3);


                //face 2
                Face face2 = new Face(
                        pointsIndex[4],
                        pointsIndex[5],
                        pointsIndex[6],
                        pointsIndex[7]);
                //bool  contains2 = faces.Contains(face2);
                //if (!contains2)
                faces.Add(face2);

                //face 1
                Face face = new Face(
                        pointsIndex[0],
                        pointsIndex[1],
                        pointsIndex[2],
                        pointsIndex[3]);
                //bool  Contains = faces.Contains(face);
                //if (!Contains)
                faces.Add(face);

                //添加面的中心变量
                for (int j = 0; j < varNames.Count(); j++)
                {
                    String varN = varNames[j];
                    float vaql = vars[varN][i];//当前单元的中心变量值
                    for (int h = 0; h < 6; h++)
                    {
                        faceCenterVar[varN].Add(vaql);
                    }
                }
            }
            mianform.returnNum++;
        }
        /**
         * 面网格数据
         * @param
         * @param v1
         * @param
         * @param vars
         */
        public void Face()
        {
            ZoneData zoneData = contourZone.zoneData;
            Zone zone = new Zone();
            Dictionary<String, List<float>> varMap = zone.getVarMap();
            Dictionary<String, List<int>> varCiShuMap = zone.getVarCiShuMap();
            List<P3D> points = zone.getPoints();
            List<Face> faces = zone.getFaces();
            Dictionary<String, List<float>> faceCenterVar = zone.getfaceCenterValueMap();
            contourZone.zones.Add(zone);

            String[] varNames = vars.Keys.ToArray();

            for (int i = 0; i < varNames.Count(); i++)
            {
                //k 叠加的值 v 叠加次数
                List<float> list = new List<float>();
                varMap.Add(varNames[i], list);
                List<int> cishulist = new List<int>();
                varCiShuMap.Add(varNames[i], cishulist);
                List<float> listcenter = new List<float>();
                faceCenterVar.Add(varNames[i], listcenter);
            }

            bool boolean = false;
            for (int i = 0; i < zoneData.centerX_LS.Count(); i++)
            {
                float x = zoneData.centerX_LS[i];
                float y = zoneData.centerY_LS[i];
                float z = zoneData.centerZ_LS[i];
                boolean = false;

                if (indexVal < 2 && contourZone.getValLimits()[indexVal] == x)
                {
                    boolean = true;
                }
                else if (indexVal >= 4 && contourZone.getValLimits()[indexVal] == z)
                {
                    boolean = true;
                }
                else if (contourZone.getValLimits()[indexVal] == y)
                {
                    boolean = true;
                }


                if (!boolean)
                    continue;
                float x1 = x + xlen;
                float y1 = y + ylen;
                float z1 = z + zlen;

                float x2 = x - xlen;
                float y2 = y - ylen;
                float z2 = z - zlen;

                P3D[] p3ds = new P3D[bs];
                p3ds[0] = new P3D(x1, y1, z1);
                p3ds[1] = new P3D(x1, y1, z2);
                p3ds[2] = new P3D(x1, y2, z2);
                p3ds[3] = new P3D(x1, y2, z1);

                p3ds[4] = new P3D(x2, y1, z1);
                p3ds[5] = new P3D(x2, y1, z2);
                p3ds[6] = new P3D(x2, y2, z2);
                p3ds[7] = new P3D(x2, y2, z1);
                int[] pointsIndex = new int[bs];


                //异次去重法
                for (int k = 0; k < p3ds.Count(); k++)
                {
                    P3D p3d = p3ds[k];
                    //if (points.Contains(p3d))
                    //{
                    //    int index = points.IndexOf(p3d);//当前重复点的下标  重复点必然是下一个单元的某个顶点
                    //    pointsIndex[k] = index;
                    //    //重复顶点变量 叠加计算
                    //    for (int j = 0; j < varNames.Count(); j++)
                    //    {
                    //        String varName = varNames[j];
                    //        float val = vars[varName][i];//当前单元的中心变量值
                    //        List<float> floats = varMap[varName];
                    //        float v = floats[index] + val; //叠加
                    //        floats.Insert(index, v);
                    //        List<int> indexList = varCiShuMap[ varName];
                    //        indexList.Insert(index, indexList[index] + 1);//叠加次数加1
                    //    }

                    //}
                    //else
                    {
                        pointsIndex[k] = points.Count();
                        points.Add(p3d);
                        //非重复顶点下 添加当前单元的变量值
                        for (int j = 0; j < varNames.Count(); j++)
                        {
                            String varName = varNames[j];
                            float val = vars[varName][i];//当前单元的中心变量值

                            List<float> floats = varMap[varName];
                            floats.Add(val);
                            List<int> indexList = varCiShuMap[varName];
                            indexList.Add(1);//叠加次数加1 

                        }

                    }
                }
                //生成单元索引
                //face 7
                Face face7 = new Face(
                        pointsIndex[2],
                        pointsIndex[3],
                        pointsIndex[7],
                        pointsIndex[6]);
                //bool  contains7 = faces.Contains(face7);
                //if (!contains7)
                faces.Add(face7);

                //face 6
                Face face6 = new Face(
                        pointsIndex[1],
                        pointsIndex[2],
                        pointsIndex[6],
                        pointsIndex[5]);
                //bool contains6 = faces.Contains(face6);
                //if (!contains6)
                faces.Add(face6);


                //face 4
                Face face4 = new Face(
                        pointsIndex[0],
                        pointsIndex[4],
                        pointsIndex[5],
                        pointsIndex[1]);
                //bool  contains4 = faces.Contains(face4);
                //if (!contains4)
                faces.Add(face4);
                //face 3
                Face face3 = new Face(
                        pointsIndex[0],
                        pointsIndex[3],
                        pointsIndex[7],
                        pointsIndex[4]);
                //bool  contains3 = faces.Contains(face3);
                //if (!contains3)
                faces.Add(face3);


                //face 2
                Face face2 = new Face(
                        pointsIndex[4],
                        pointsIndex[5],
                        pointsIndex[6],
                        pointsIndex[7]);
                //bool  contains2 = faces.Contains(face2);
                //if (!contains2)
                faces.Add(face2);

                //face 1
                Face face = new Face(
                        pointsIndex[0],
                        pointsIndex[1],
                        pointsIndex[2],
                        pointsIndex[3]);
                //bool  Contains = faces.Contains(face);
                //if (!Contains)
                faces.Add(face);

                //添加面的中心变量
                for (int j = 0; j < varNames.Count(); j++)
                {
                    String varName = varNames[j];
                    float val = vars[varName][i];//当前单元的中心变量值
                    for (int h = 0; h < 6; h++)
                    {
                        faceCenterVar[varName].Add(val);
                    }
                }

            }
            mianform.returnNum++;
        }



        Dictionary<double, List<int>> mapX = new Dictionary<double, List<int>>();
        Dictionary<double, List<int>> mapY = new Dictionary<double, List<int>>();
        Dictionary<double, List<int>> mapZ = new Dictionary<double, List<int>>();
        /**
         * 提网格区间数据提取
         * */
        public void borderNewLimit()
        {
            String varName = mianform.varNow;
            float max = mianform.varlimitData[1];
            float min = mianform.varlimitData[0];


            ZoneData zoneData = contourZone.zoneData;
            Zone zone = new Zone();
            Dictionary<String, List<float>> varMap = zone.getVarMap();
            List<P3D> points = zone.getPoints();
            List<Face> faces = zone.getFaces();
            Dictionary<String, List<float>> faceCenterVar = zone.getfaceCenterValueMap();


            String[] varNames = vars.Keys.ToArray();

            for (int i = 0; i < varNames.Count(); i++)
            {
                //k 叠加的值 v 叠加次数
                List<float> list = new List<float>();
                varMap.Add(varNames[i], list);
                //List<int> cishulist = new List<int>();
                //varCiShuMap.Add(varNames[i], cishulist);
                List<float> listcenter = new List<float>();
                faceCenterVar.Add(varNames[i], listcenter);
            }

            bool boolean = false;
            for (int i = 0; i < zoneData.centerX_LS.Count(); i++)
            {

                float val = vars[varName][i];//当前单元的中心变量值
                if (val > max || val < min)//获取区间单元
                {
                    continue;
                }

                float x = zoneData.centerX_LS[i];
                float y = zoneData.centerY_LS[i];
                float z = zoneData.centerZ_LS[i];

                P3D p3d = new P3D(x, y, z);
                points.Add(p3d);
                int index = points.Count - 1;
                //非重复顶点下 添加当前单元的变量值
                List<float> floats = varMap[varName];
                floats.Add(val);

                if (!mapX.ContainsKey(x))
                {
                    mapX.Add(x, new List<int>());

                }
                if (!mapY.ContainsKey(y))
                {
                    mapY.Add(y, new List<int>());

                }

                mapX[x].Add(index);
                mapY[y].Add(index);


            }

            if (mapX.Count > 0 || mapY.Count > 0)
            {
                // Dictionary<double, Dictionary<float, List<int>>> norMap = new Dictionary<double, Dictionary<float, List<int>>>();
                listPoints.Clear();
                //  沿着x y z两两计算
                foreach (var item in mapX)//沿着X 方向 求出外围的YZ极值点
                {
                    List<int> listx = item.Value;
                    Dictionary<float, List<int>> mapZLS = changeDataXY_Z(listx, points);
                    addFaceAndCenterVar_Z(faces, faceCenterVar[varName], mapZLS, points, varMap[varName]);

                    Dictionary<float, List<int>> mapYLS = changeDataXZ_Y(listx, points);
                    addFaceAndCenterVar_Y(faces, faceCenterVar[varName], mapYLS, points, varMap[varName]);
                    listx.Clear();
                    // norMap.Add(item.Key, mapYLS);
                }

                foreach (var item in mapY)//沿着X 方向 求出外围的YZ极值点
                {
                    List<int> listx = item.Value;
                    Dictionary<float, List<int>> mapXLS = changeDataYZ_X(listx, points);
                    addFaceAndCenterVar_X(faces, faceCenterVar[varName], mapXLS, points, varMap[varName]);
                    listx.Clear();
                }
                points.Clear();
                zone.setPoints(listPoints);

                contourZone.zones.Add(zone);
            }
            mianform.returnNum++;

        }
        List<P3D> listPoints = new List<P3D>();
        private void addFaceAndCenterVar_Z(List<Face> faces, List<float> varlist,
            Dictionary<float, List<int>> mapZLS, List<P3D> p3dlist, List<float> vars)
        {

            //throw new NotImplementedException(); 
            foreach (var item in mapZLS)//Z方向的平面
            {
                int indexp3d = item.Value[0];//最大的 z 
                P3D p3d = p3dlist[indexp3d];
                float z = p3d.getZ();
                float x1 = p3d.getX() + xlen;
                float x2 = p3d.getX() - xlen;
                float y1 = p3d.getY() + ylen;
                float y2 = p3d.getY() - ylen;

                P3D p1 = new P3D(x1, y1, z);
                P3D p2 = new P3D(x2, y1, z);
                P3D p3 = new P3D(x2, y2, z);
                P3D p4 = new P3D(x1, y2, z);
                listPoints.Add(p1);//创建定点
                listPoints.Add(p2);
                listPoints.Add(p3);
                listPoints.Add(p4);
                int index = listPoints.Count() - 4;
                faces.Add(new Face(index, index + 1, index + 2, index + 3));//添加单元
                                                                            //   varlist.Add(vars[indexp3d]);//记录单元变量 
                float varvalue = vars[indexp3d];
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 

                ///////////////////////////////////////
                indexp3d = item.Value[1];//最大的 z 
                p3d = p3dlist[indexp3d];
                z = p3d.getZ();
                x1 = p3d.getX() + xlen;
                x2 = p3d.getX() - xlen;
                y1 = p3d.getY() + ylen;
                y2 = p3d.getY() - ylen;

                p1 = new P3D(x1, y1, z);
                p2 = new P3D(x2, y1, z);
                p3 = new P3D(x2, y2, z);
                p4 = new P3D(x1, y2, z);
                listPoints.Add(p1);//创建定点
                listPoints.Add(p2);
                listPoints.Add(p3);
                listPoints.Add(p4);
                index = listPoints.Count() - 4;
                faces.Add(new Face(index, index + 1, index + 2, index + 3));//添加单元
                                                                            //   varlist.Add(vars[indexp3d]);//记录单元变量 
                varvalue = vars[indexp3d];
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 
            }
        }

        private void addFaceAndCenterVar_Y(List<Face> faces, List<float> varlist,
            Dictionary<float, List<int>> mapZLS, List<P3D> p3dlist, List<float> vars)
        {

            //throw new NotImplementedException(); 
            foreach (var item in mapZLS)//Z方向的平面
            {
                int indexp3d = item.Value[0];//最大的 Y  
                P3D p3d = p3dlist[indexp3d];
                float z = p3d.getY();
                float x1 = p3d.getX() + xlen;
                float x2 = p3d.getX() - xlen;
                float y1 = p3d.getZ() + ylen;
                float y2 = p3d.getZ() - ylen;

                P3D p1 = new P3D(x1, z, y1);
                P3D p2 = new P3D(x2, z, y1);
                P3D p3 = new P3D(x2, z, y2);
                P3D p4 = new P3D(x1, z, y2);
                listPoints.Add(p1);//创建定点
                listPoints.Add(p2);
                listPoints.Add(p3);
                listPoints.Add(p4);
                int index = listPoints.Count() - 4;
                faces.Add(new Face(index, index + 1, index + 2, index + 3));//添加单元
                                                                            //   varlist.Add(vars[indexp3d]);//记录单元变量 
                float varvalue = vars[indexp3d];
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 
                ///////////////////////////////////////////////////////////
                indexp3d = item.Value[1];//最大的 Y  
                p3d = p3dlist[indexp3d];
                z = p3d.getY();
                x1 = p3d.getX() + xlen;
                x2 = p3d.getX() - xlen;
                y1 = p3d.getZ() + ylen;
                y2 = p3d.getZ() - ylen;

                p1 = new P3D(x1, z, y1);
                p2 = new P3D(x2, z, y1);
                p3 = new P3D(x2, z, y2);
                p4 = new P3D(x1, z, y2);
                listPoints.Add(p1);//创建定点
                listPoints.Add(p2);
                listPoints.Add(p3);
                listPoints.Add(p4);
                index = listPoints.Count() - 4;
                faces.Add(new Face(index, index + 1, index + 2, index + 3));//添加单元
                                                                            //   varlist.Add(vars[indexp3d]);//记录单元变量 
                varvalue = vars[indexp3d];
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 
            }
        }


        private void addFaceAndCenterVar_X(List<Face> faces, List<float> varlist,
         Dictionary<float, List<int>> mapZLS, List<P3D> p3dlist, List<float> vars)
        {

            //throw new NotImplementedException(); 
            foreach (var item in mapZLS)//Z方向的平面
            {
                int indexp3d = item.Value[0];//最大的 X 
                P3D p3d = p3dlist[indexp3d];
                float z = p3d.getX();
                float x1 = p3d.getZ() + zlen;
                float x2 = p3d.getZ() - zlen;
                float y1 = p3d.getY() + ylen;
                float y2 = p3d.getY() - ylen;

                P3D p1 = new P3D(z, y1, x1);
                P3D p2 = new P3D(z, y1, x2);
                P3D p3 = new P3D(z, y2, x2);
                P3D p4 = new P3D(z, y2, x1);
                listPoints.Add(p1);//创建定点
                listPoints.Add(p2);
                listPoints.Add(p3);
                listPoints.Add(p4);
                int index = listPoints.Count() - 4;
                //faces.Add(new Face(index, index + 1, index + 2, index + 3));//添加单元
                faces.Add(new Face(index + 3, index + 2, index + 1, index));//添加单元

                //   varlist.Add(vars[indexp3d]);//记录单元变量 
                float varvalue = vars[indexp3d];
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 

                /////////////////////////////////////////
                indexp3d = item.Value[1];//最大的 X 
                p3d = p3dlist[indexp3d];
                z = p3d.getX();
                x1 = p3d.getZ() + zlen;
                x2 = p3d.getZ() - zlen;
                y1 = p3d.getY() + ylen;
                y2 = p3d.getY() - ylen;

                p1 = new P3D(z, y1, x1);
                p2 = new P3D(z, y1, x2);
                p3 = new P3D(z, y2, x2);
                p4 = new P3D(z, y2, x1);
                listPoints.Add(p1);//创建定点
                listPoints.Add(p2);
                listPoints.Add(p3);
                listPoints.Add(p4);
                index = listPoints.Count() - 4;
                //faces.Add(new Face(index, index + 1, index + 2, index + 3));//添加单元
                faces.Add(new Face(index + 3, index + 2, index + 1, index));//添加单元

                //   varlist.Add(vars[indexp3d]);//记录单元变量 
                varvalue = vars[indexp3d];
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 
                varlist.Add(varvalue);//记录顶点变量 
            }
        }

        /**
         * 获取x截面上z外围点
         * */
        private Dictionary<float, List<int>> changeDataXY_Z(List<int> listx, List<P3D> points)
        {
            Dictionary<float, List<int>> mapYLS = new Dictionary<float, List<int>>();
            for (int i = 0; i < listx.Count; i++)
            {
                int p3dindex = listx[i];
                P3D p3d = points[p3dindex];
                float y = p3d.getY();

                float z = p3d.getZ();

                if (!mapYLS.ContainsKey(y))
                {
                    mapYLS.Add(y, new List<int>());
                    mapYLS[y].Add(p3dindex);
                    continue;
                }

                if (mapYLS[y].Count == 1)//获取 当前 X y行的Z极值
                {
                    P3D lsp = points[mapYLS[y][0]];
                    if (lsp.getZ() < z)
                    {
                        mapYLS[y].Insert(0, p3dindex);
                    }
                    else
                    {
                        mapYLS[y].Add(p3dindex);
                    }
                }
                else
                {
                    P3D lsp = points[mapYLS[y][0]];
                    if (z > lsp.getZ())
                    {
                        mapYLS[y].RemoveAt(0);
                        mapYLS[y].Insert(0, p3dindex);
                    }
                    lsp = points[mapYLS[y][1]];
                    if (z < lsp.getZ())
                    {
                        mapYLS[y].RemoveAt(1);
                        mapYLS[y].Add(p3dindex);
                    }

                }

            }


            return mapYLS;
        }


        /**
        * 获取x截面上Y外围点
        * */
        public Dictionary<float, List<int>> changeDataXZ_Y(List<int> listx, List<P3D> points)
        {
            Dictionary<float, List<int>> mapYLS = new Dictionary<float, List<int>>();
            for (int i = 0; i < listx.Count; i++)
            {
                int p3dindex = listx[i];
                P3D p3d = points[p3dindex];
                float y = p3d.getY();

                float z = p3d.getZ();

                if (!mapYLS.ContainsKey(z))
                {
                    mapYLS.Add(z, new List<int>());
                    mapYLS[z].Add(p3dindex);
                    continue;
                }

                if (mapYLS[z].Count == 1)//获取 当前 X y行的Z极值
                {
                    P3D lsp = points[mapYLS[z][0]];
                    if (lsp.getY() < y)
                    {
                        mapYLS[z].Insert(0, p3dindex);
                    }
                    else
                    {
                        mapYLS[z].Add(p3dindex);
                    }
                }
                else
                {
                    P3D lsp = points[mapYLS[z][0]];
                    if (y > lsp.getY())
                    {
                        mapYLS[z].RemoveAt(0);
                        mapYLS[z].Insert(0, p3dindex);
                    }
                    lsp = points[mapYLS[z][1]];
                    if (y < lsp.getY())
                    {
                        mapYLS[z].RemoveAt(1);
                        mapYLS[z].Add(p3dindex);
                    }

                }

            }


            return mapYLS;
        }



        /**
        * 获取x截面上Y外围点
        * */
        public Dictionary<float, List<int>> changeDataYZ_X(List<int> listx, List<P3D> points)
        {
            Dictionary<float, List<int>> mapYLS = new Dictionary<float, List<int>>();
            for (int i = 0; i < listx.Count; i++)
            {
                int p3dindex = listx[i];
                P3D p3d = points[p3dindex];
                float y = p3d.getX();

                float z = p3d.getZ();

                if (!mapYLS.ContainsKey(z))
                {
                    mapYLS.Add(z, new List<int>());
                    mapYLS[z].Add(p3dindex);
                    continue;
                }

                if (mapYLS[z].Count == 1)//获取 当前 X y行的Z极值
                {
                    P3D lsp3d = points[mapYLS[z][0]];
                    if (lsp3d.getX() < y)
                    {
                        mapYLS[z].Insert(0, p3dindex);
                    }
                    else
                    {
                        mapYLS[z].Add(p3dindex);
                    }
                }
                else
                {
                    P3D lsp3d = points[mapYLS[z][0]];
                    if (y > lsp3d.getX())
                    {
                        mapYLS[z].RemoveAt(0);
                        mapYLS[z].Insert(0, p3dindex);
                    }
                    lsp3d = points[mapYLS[z][1]];
                    if (y < lsp3d.getX())
                    {
                        mapYLS[z].RemoveAt(1);
                        mapYLS[z].Add(p3dindex);
                    }

                }

            }


            return mapYLS;
        }
    }
}
